package com.wn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wn.bean.Permission;
import com.wn.bean.RolePermission;
import com.wn.mapper.PermissionMapper;
import com.wn.mapper.RolePermissionMapper;
import com.wn.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.reflection.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yellow Docter
 * @since 2022-05-16
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;


//    var zNodes =[
//            *         { id:1, pId:0, name:"系统操作", open:true},
//            *         { id:11, pId:1, name:"用户管理", open:true},
//            *         { id:111, pId:11, name:"用户管理添加"},
//            *         { id:112, pId:11, name:"用户管理删除"},
//            *         { id:12, pId:1, name:"角色管理", open:true},
//            *         { id:121, pId:12, name:"角色管理添加"},
//            *         { id:122, pId:12, name:"角色管理删除"},
//            *         { id:2, pId:0, name:"随意勾选 2", checked:true, open:true},
//            *         { id:21, pId:2, name:"随意勾选 2-1"},
//            *         { id:22, pId:2, name:"随意勾选 2-2", open:true},
//            *         { id:221, pId:22, name:"随意勾选 2-2-1", checked:true},
//            *         { id:222, pId:22, name:"随意勾选 2-2-2"},
//            *         { id:23, pId:2, name:"随意勾选 2-3"}
//          ];
    /**
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findPermission(Integer rid) {

        List<Map<String,Object>> list = new ArrayList<>();

        // 01 所有的权限查询出来
        List<Permission> permissions = permissionMapper.selectList(null);

        //02 把 permissions 转成一个map

        //03 通过rid 查询  这个角色对应的权限

        QueryWrapper<RolePermission> qr = new QueryWrapper<>();
        //通过条件 rid 查询 RolePermission
        qr.eq("rid",rid);

//        // select() 查询指定字段
        qr.select("pid");

        // 只返回 第一个字段
        List<Object> pids = rolePermissionMapper.selectObjs(qr);

        for (Permission permission : permissions) {

            Map<String,Object> map = new HashMap();

            map.put("id",permission.getPid());
            map.put("pId",permission.getParentid());
            map.put("name",permission.getPname());

            // select count(*) from t_permission where parentid = 4;
            // map.put("open",逻辑判断);

            for (Object pid : pids) {
                // 角色查询的 pid 和 所有的全选的pid 进行比较 如果一致
                // 勾选上
                if (Integer.parseInt(pid.toString()) == permission.getPid()){
                    map.put("checked",true);
                }
            }

            list.add(map);
        }
        return list;
    }
}
